/**
 * 
 */
package org.easybuy.daoimpol;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.easybuy.dao.BaseDao;
import org.easybuy.dao.BuyCarDao;
import org.easybuy.entity.BuyCar;

/**
 *
 * @购物车DAO
 */
public  class BuyCarDaoImpol extends BaseDao implements BuyCarDao {
	// 添加购物车
	@Override
	public int addBuyCar(BuyCar buyCar) {
		String sql = "INSERT INTO `easybuy_buycar` (`name`,price,fileName,number,userName) VALUES (?,?,?,?,?)";
		Object[] parm = { buyCar.getName(), buyCar.getPrice(),
				buyCar.getFileName(), buyCar.getNumber(), buyCar.getUserName() };
		int result = this.executeUpdate(sql, parm);
		return result;
	}
	
	// 根据用户查询购物车详情

	public List<BuyCar> getAllByUserName(String UserName) {
		List<BuyCar> list = new ArrayList<BuyCar>();
		ResultSet rs = null;
		BuyCar buyCar = null;
		String sql = "SELECT *FROM `easybuy_buycar` WHERE username = ?";
		rs = this.exxcuteQuery(sql, UserName);
		try {
			while (rs.next()) {
				buyCar = new BuyCar();
				buyCar.setId(rs.getInt("id"));
				buyCar.setName(rs.getString("name").toString());
				buyCar.setPrice(rs.getString("price").toString());
				buyCar.setNumber(rs.getString("number").toString());
				buyCar.setFileName(rs.getString("fileName").toString());
				list.add(buyCar);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(connection, pstmt, rs);
		}
		return list;
	}

	// 根据商品ID修改数量
	@Override
	public int upDateNumber(BuyCar buyCar) {
		// TODO Auto-generated method stub
		String sql = "UPDATE `easybuy_buycar` SET number=?,price = ? WHERE id = ?";
		Object[] parm = { buyCar.getNumber(), buyCar.getPrice(), buyCar.getId() };
		int result = this.executeUpdate(sql, parm);
		return result;
	}

	// 计算总金额数
	@Override
	public String zPrice(String UserName) {
		// TODO Auto-generated method stub
		ResultSet rs = null;
		BuyCar buyCar = null;
		String price = null;
		String sql = "SELECT SUM(price) AS SUM_PRICE FROM `easybuy_buycar` WHERE userName = ?";
		try {
			buyCar = new BuyCar();
			rs = this.exxcuteQuery(sql, UserName);
			rs.next();
			price = rs.getString("SUM_PRICE");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(connection, pstmt, rs);
		}
		return price;
	}

	// 计算购物车总数目
	@Override
	public int sumBuyCar(String UserName) {
		ResultSet rs = null;
		String sql = "SELECT COUNT(1) FROM `easybuy_buycar` where userName = ?";
		int count = -1;
		try {
			rs = this.exxcuteQuery(sql, UserName);
			rs.next();
			count = rs.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(connection, pstmt, rs);
		}
		return count;
	}

	// 删除购物车商品
	@Override
	public int dropBuyCar(BuyCar buyCar) {
		String sql = "DELETE FROM `easybuy_buycar` WHERE id = ?";
		Object[] parm = { buyCar.getId() };
		int result = this.executeUpdate(sql, parm);
		return result;
	}
}
